﻿import pygame
from pygame.locals import *

from model import World
from view import WorldView

def WorldController():

    world = World((80, 60))
    worldview  = WorldView(world)

    running = True
    active  = False
    painting = True

    while running:

        event = pygame.event.poll()

        if event.type == QUIT:
            running = False

        elif event.type == KEYUP:

            if event.key == K_ESCAPE:
                running = False

            elif event.key == K_r:
                world.randomize()

            elif event.key == K_c:
                world.clear()

            elif event.key == K_s:
                active = not active

        elif event.type == MOUSEBUTTONDOWN:
            x, y = event.pos
            x = x / worldview.size
            y = y / worldview.size
            if event.button == 1:
                world.cells[x][y] = 1
            elif event.button == 3:
                world.cells[x][y] = 0

        elif event.type == MOUSEMOTION:
            x, y = event.pos
            x = x / worldview.size
            y = y / worldview.size
            a, b, c = event.buttons
            if (a == 1) and (c != 1):
                world.cells[x][y] = 1
            elif (a != 1) and (c == 1):
                world.cells[x][y] = 0

        if active:
            world.deliver_fate()

        worldview.draw()
